package demo.practice.leetcode_eveyday;

import demo.practice.TreeNode;

import java.util.HashMap;
import java.util.Map;

public class P993 {


    public static void main(String[] args) {
        //P993 p993= new P993();
        //p993.isCousins()
    }

    Map<Integer, Integer> heights = new HashMap<>();
    Map<Integer, TreeNode> parents = new HashMap<>();


    public boolean isCousins(TreeNode root, int x, int y) {
        getHeights(root, 0);
        return   heights.get(x) != null && heights.get(y) != null && heights.get(x).equals(heights.get(y)) && parents.get(x)!=parents.get(y)  ;
    }


    private void getHeights(TreeNode root, int height) {
        if (root == null)
            return;

        heights.put(root.val, height);
        if(root.left!=null){
            parents.put(root.left.val,root);
            getHeights(root.left, height + 1);
        }

        if(root.right!=null){
            parents.put(root.right.val,root);
            getHeights(root.right, height + 1);
        }

    }
}
